package com.itheima.company.service.impl;

import com.alibaba.fastjson.JSON;
import com.itheima.company.mapper.DepetmentMapper;
import com.itheima.company.pojo.Company;
import com.itheima.company.pojo.Department;
import com.itheima.company.pojo.vo.DeptListResult;
import com.itheima.company.service.CompanyService;
import com.itheima.company.service.DepartmentService;
import com.itheima.common.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author IntelliJ IDEA
 * @date 2020/10/22  9:25
 */
@Service
@Transactional
public class DepartmentImpl implements DepartmentService {

    @Autowired
    private DepetmentMapper depetmentMapper;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private IdWorker idWorker;


    /**
     * 查询department全部列表
     */
    @Override
    public DeptListResult findAllDepartmentByCompanyId(String companyId,String companyName) {
        //根据companyId查询企业
        Company company = companyService.findCompanyById(companyId);
        //根据companyId查新企业下所有的部门
        List<Department> departmentList=depetmentMapper.findAllDepartmentByCompanyId(companyId);
        //封装数据返回
        return new DeptListResult(company,departmentList);
    }

    /**
     *  根据id查询部门
     */
    @Override
    public Department findDepartmentById(String id) {
        return depetmentMapper.findDepartmentById(id);
    }

    /**
     * 根据id删除部门
     */
    @Override
    public Integer deleteDepartmentById(String id) {
        return depetmentMapper.deleteDepartmentById(id);
    }

    /**
     * 修改部门
     */
    @Override
    public Integer updateDepartment(Department department) {
        return depetmentMapper.updateDepartment(department);
    }

    /**
     * 新增部门
     */
    @Override
    public Integer addDepartment(Department department) {
        return depetmentMapper.addDepartment(department);
    }

    /**
     * 根据部门编码以及企业id查询部门信息
     */
    @Override
    public Department findByCode(String code, String companyId) {
        return depetmentMapper.findDepartmentByCodeAndCompanyId(code,companyId);
    }

}
